package com.food.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;
import org.springframework.beans.factory.annotation.Value;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

@Component
public class ResourceInitializer implements CommandLineRunner {
    private static final Logger logger = LoggerFactory.getLogger(ResourceInitializer.class);

    @Value("${file.upload.path}")
    private String uploadPath;

    @Override
    public void run(String... args) throws Exception {
        // 确保静态资源目录存在
        Path staticDir = Paths.get("src/main/resources/static/images/avatar");
        if (!Files.exists(staticDir)) {
            Files.createDirectories(staticDir);
            logger.info("Created avatar directory: {}", staticDir);
        }

        // 创建预设头像
        String[] presetAvatars = {
            "default.jpg",
            "avatar1.jpg",
            "avatar2.jpg",
            "avatar3.jpg"
        };

        for (String avatarName : presetAvatars) {
            Path avatarPath = staticDir.resolve(avatarName);
            if (!Files.exists(avatarPath)) {
                // 创建一个简单的默认头像（1x1像素的JPG）
                byte[] defaultImageBytes = {
                    (byte)0xFF, (byte)0xD8, (byte)0xFF, (byte)0xE0, 0x00, 0x10, 0x4A, 0x46,
                    0x49, 0x46, 0x00, 0x01, 0x01, 0x01, 0x00, 0x48, 0x00, 0x48, 0x00, 0x00,
                    (byte)0xFF, (byte)0xDB, 0x00, 0x43, 0x00, (byte)0xFF, (byte)0xFF, (byte)0xFF,
                    (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
                    (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF, (byte)0xFF,
                    (byte)0xFF, (byte)0xC0, 0x00, 0x0B, 0x08, 0x00, 0x01, 0x00, 0x01, 0x01,
                    0x01, 0x11, 0x00, (byte)0xFF, (byte)0xC4, 0x00, 0x14, 0x00, 0x01, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, (byte)0xFF, (byte)0xC4, 0x00, 0x14, 0x10, 0x01, 0x00,
                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
                    0x00, 0x00, 0x00, (byte)0xFF, (byte)0xDA, 0x00, 0x08, 0x01, 0x01, 0x00,
                    0x00, 0x3F, 0x00, 0x7F, 0x00, (byte)0xFF, (byte)0xD9
                };
                Files.write(avatarPath, defaultImageBytes);
                logger.info("Created preset avatar: {}", avatarPath);
            }
        }

        // 确保上传目录存在
        Path uploadDir = Paths.get(uploadPath);
        if (!Files.exists(uploadDir)) {
            Files.createDirectories(uploadDir);
            logger.info("Created upload directory: {}", uploadDir);
        }
    }
} 